<?php

require_once 'login.php';
require_once 'conexion.php';
require_once 'objects/person.php';
require_once 'actions/actionsEnterDisease.php';
require_once 'actions/actionsEnterFood.php';
require_once 'actions/actionsEnterRace.php';
require_once 'actions/actionsEnterLand.php';
require_once 'actions/actionsEnterBatch.php';
require_once 'actions/actionsEnterCattle.php';
require_once 'actions/BatchProm.php';

new servlet();

class servlet {
    
    private $conx;

    public function __construct() {
        $action = $_GET['action'];
        if ($action == 'singin') {
            $this->loguear();
        } else if($action == 'enterDisease'){
            $this->regDisease();
        } else if($action == 'regFood'){
            $this->regFood();
        } else if($action == 'enterRace'){
            $this->regRace();
        } else if($action  == 'enterLand'){
            $this->regLand();
        } else if ($action == 'enterbatch'){
            $this->regBatch();  
        } else if ($action == 'register'){
            $this->regUser();
        } else if ($action == 'enterCattlePD1'){
            //$this->regCattlePD1();
            $this->saveCattlePD1();
        } else if ($action == 'enterCattlePD2'){
            $this->regCattlePD2();
        } else if ($action == 'enterCattlePD3'){
            $this->regCattlePD3();
        } else if ($action == 'enterCattlePC1'){
            $this->regCattlePC1();
        } else if ($action == 'enterCattlePC2'){
            $this->regCattlePC2();
        } else if ($action == 'enterCattlePC3'){
            $this->regCattlePC3();
        } else if ($action == 'enterCattleC1'){
            $this->regCattleC1();
        } else if ($action == 'enterCattleC2'){
            $this->regCattleC2();
        } else if ($action == 'enterCattleC3'){
            $this->regCattleC3();
        } else if ($action == 'regGrowthUser'){
            $this->regGrowthUser();
        } else if ($action == 'updatePerson'){
            $this->updatePerson();
        }else {
            header("");
        }
    }

    public function loguear() {
        $log = new login();
        $log->login($_POST['email'], $_POST['passwd']);
    }
 
        public function regDisease(){
        $disease = new actionsEnterDiseace();
        $diseasecod = $disease->thisdisease();
        $diseasename = $_POST['namedisease'];
        $symptom = $_POST['symptoms'];
        $tratamient = $_POST['tratamient'];
        $sql = "INSERT INTO DISEASE VALUES (".$diseasecod.",
                                            '".$diseasename."',
                                            '".$symptom."',
                                            '".$tratamient."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: adm/menuadm.php");
    }
    
    public function regFood(){
        $food = new actionsEnterFood();
        $foodcod = $food->thisfood();
        $namefood = $_POST['namefood'];
        $drymatter = $_POST['dryMatterFood'];
        $crudeprotein = $_POST['crudeProtein'];
        $ndt = $_POST['ndt'];
        $em = $_POST['em'];
        $enm = $_POST['enm'];
        $eng = $_POST['eng'];
        $Ca = $_POST['calcium'];
        $P = $_POST['phosphorus'];
        $sql = "INSERT INTO FOOD VALUES (".$foodcod.",
                                        '".$namefood."',
                                         ".$drymatter.",
                                         ".$crudeprotein.",
                                         ".$ndt.",
                                         ".$em.",
                                         ".$enm.",
                                         ".$eng.",
                                         ".$Ca.",
                                         ".$P.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: adm/menuadm.php");
    }
    
    public function regRace(){
        $race = new actionsEnterRace();
        $racecod = $race->thisrace();
        $namerace = $_POST['namerace'];
        $weightbirth = $_POST['birthWeight'];
        $weightweaning = $_POST['weaningWeight'];
        $weightsacr = $_POST['sacrWeight'];
        $ageweanig = $_POST['weaningAge'];
        $agesacr = $_POST['sacrAge'];
        $drymatter = $_POST['dryMatter'];
        $tenderness = $_POST['tenderness'];
        $canalperformance = $_POST['efficiency'];
        $weightcanal = ($weightsacr*$canalperformance)/100;
        $beforeweaning = ($weightweaning-$weightbirth)/$ageweanig;
        $afterweaning = (($weightsacr-$weightweaning)/($agesacr-$ageweanig));
        $imagerace = $_POST['imgrace'];
        $sql = "INSERT INTO RACE VALUES (".$racecod.",
                                        '".$namerace."',
                                        ".$weightbirth.",
                                        ".$weightweaning.",
                                        ".$weightsacr.",
                                        ".$ageweanig.",
                                        ".$agesacr.",
                                        ".$drymatter.",
                                        ".$tenderness.",
                                        ".$canalperformance.",
                                        ".$weightcanal.",
                                        ".$beforeweaning.",
                                        ".$afterweaning.",
                                        '".$imagerace."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: adm/menuadm.php");      
    }
    
    public function regLand(){
        $land = new actionsEnterLand();
        $landcod = $land->thisland();
        $landname = $_POST['lname'];
        $departament = $_POST['select'];
        $township = $_POST['select2'];
        $sidewalk = $_POST['swalk'];
        $userid = $_POST['userid'];
        $sql = "INSERT INTO LAND VALUES (".$landcod.",
                                        '".$landname."',
                                        '".$departament."',
                                        '".$township."',
                                        '".$sidewalk."',
                                         ".$userid.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/Registers.php");
    }
  
    public function regBatch(){
        $batch = new actionsEnterBatch();
        $d = date('d-m-Y');
        $batchcod = $batch->thisbatch();        
        $animalnum = $_POST['animalbatch'];
        $batchname = $_POST['bname'];
        $foodbatch = $_POST['fbatch'];
        $racebatch = $_POST['racebatch'];
        $landcod = $_POST['landcod'];
        $dateregisterb =  $d;
        $sql = "INSERT INTO BATCH VALUES (".$batchcod.",
                                          ".$animalnum.",
                                         '".$batchname."',
                                          ".$foodbatch.",
                                          ".$racebatch.",
                                          ".$landcod.",
                                          '".$dateregisterb."');";                                          
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/Registers.php");    
    }
    
    public function regUser(){
        $nameuser = $_POST['name'];
        $lastname = $_POST['last_name'];
        $doctype = $_POST['doctype'];
        $docnum = $_POST['numdoc'];
        $mail = $_POST['email'];
        $passwd = $_POST['passwd2'];
        $address = $_POST['address'];
        $phone = $_POST['phone'];
        $rolperson = 01;
        $sql = "INSERT INTO PERSON VALUES ('".$nameuser."',
                                          '".$lastname."',
                                          '".$doctype."',
                                           ".$docnum.",
                                          '".$mail."',
                                          '".$passwd."',
                                          '".$address."',
                                           ".$phone.",
                                           ".$rolperson.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: ../contact.php");      
    }
    
    public function saveCattlePD1(){
        $predeste=new Predeste();
        $predeste->setAgeAct($_POST['ageAct1']);
        $predeste->setBatchCod($_POST['batch1']);
        $predeste->setBirthdayCa($_POST['date1']);
        $predeste->setWeightbirth($_POST['birthWeight1']);
        $predeste->setWeighthAct($_POST['weigthAct1']);
        $_SESSION['predeste1']=$predeste;
        header("location: user/enterCattlePD_1.php"); 
    }
    
    public function regCattlePD1(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();        
        $birthdayca1 = $_POST['date1'];
        $weightbirth1 = $_POST['birthWeight1'];
        $weigthweaning = 'null';
        $weightact1 = $_POST['weigthAct1'];
        $ageWeaning = 'null';
        $ageact1 = $_POST['ageAct1'];
        $stage = 'Predestete';
        $batchcod = $_POST['batch1'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca1."',
                                            ".$weightbirth1.",
                                            ".$weigthweaning.",
                                            ".$weightact1.",
                                            ".$ageWeaning.",
                                            ".$ageact1.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');"; 
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattlePD_2.php");    
    }
    
    public function regCattlePD2(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();        
        $birthdayca2 = $_POST['date2'];
        $weightbirth2 = $_POST['birthWeight2'];
        $weigthweaning = 'null';
        $weightact2 = $_POST['weigthAct2'];
        $ageWeaning = 'null';
        $ageact2 = $_POST['ageAct2'];
        $stage = 'Predestete';
        $batchcod = $_POST['batch2'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca2."',
                                            ".$weightbirth2.",
                                            ".$weigthweaning.",
                                            ".$weightact2.",
                                            ".$ageWeaning.",
                                            ".$ageact2.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');"; 
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattlePD_3.php");    
    }
    
    public function regCattlePD3(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca3 = $_POST['date3'];
        $weightbirth3 = $_POST['birthWeight3'];
        $weigthweaning = 'null';
        $weightact3 = $_POST['weigthAct3'];
        $ageWeaning = 'null';
        $ageact3 = $_POST['ageAct3'];
        $stage = 'Predestete';
        $batchcod = $_POST['batch3'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca3."',
                                            ".$weightbirth3.",
                                            ".$weigthweaning.",
                                            ".$weightact3.",
                                            ".$ageWeaning.",
                                            ".$ageact3.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattlePC_1.php");    
    }
    
    public function regCattlePC1(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca4 = $_POST['date4'];
        $weightbirth4 = $_POST['birthWeight4'];
        $weigthweaning4 = $_POST['weigthW4'];
        $weightact4 = $_POST['weigthAct4'];
        $ageWeaning = $_POST['ageW4'];
        $ageact4 = $_POST['ageAct4'];
        $stage = 'PreCeba';
        $batchcod = $_POST['batch4'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca4."',
                                            ".$weightbirth4.",
                                            ".$weigthweaning4.",
                                            ".$weightact4.",
                                            ".$ageWeaning.",
                                            ".$ageact4.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattlePC_2.php");    
    }
    
    public function regCattlePC2(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca5 = $_POST['date5'];
        $weightbirth5 = $_POST['birthWeight5'];
        $weigthweaning5 = $_POST['weigthW5'];
        $weightact5 = $_POST['weigthAct5'];
        $ageWeaning = $_POST['ageW5'];
        $ageact5 = $_POST['ageAct5'];
        $stage = 'PreCeba';
        $batchcod = $_POST['batch5'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca5."',
                                            ".$weightbirth5.",
                                            ".$weigthweaning5.",
                                            ".$weightact5.",
                                            ".$ageWeaning.",
                                            ".$ageact5.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattlePC_3.php");    
    }
    
    public function regCattlePC3(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca6 = $_POST['date6'];
        $weightbirth6 = $_POST['birthWeight6'];
        $weigthweaning6 = $_POST['weigthW6'];
        $weightact6 = $_POST['weigthAct6'];
        $ageWeaning = $_POST['ageW6'];
        $ageact6 = $_POST['ageAct6'];
        $stage = 'PreCeba';
        $batchcod = $_POST['batch6'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca6."',
                                            ".$weightbirth6.",
                                            ".$weigthweaning6.",
                                            ".$weightact6.",
                                            ".$ageWeaning.",
                                            ".$ageact6.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattleC_1.php");    
    }
    
    public function regCattleC1(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca7 = $_POST['date7'];
        $weightbirth7 = $_POST['birthWeight7'];
        $weigthweaning7 = $_POST['weigthW7'];
        $weightact7 = $_POST['weigthAct7'];
        $ageWeaning = $_POST['ageW7'];
        $ageact7 = $_POST['ageAct7'];
        $stage = 'Ceba';
        $batchcod = $_POST['batch7'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca7."',
                                            ".$weightbirth7.",
                                            ".$weigthweaning7.",
                                            ".$weightact7.",
                                            ".$ageWeaning.",
                                            ".$ageact7.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattleC_2.php");   
}

public function regCattleC2(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca8 = $_POST['date8'];
        $weightbirth8 = $_POST['birthWeight8'];
        $weigthweaning8 = $_POST['weigthW8'];
        $weightact8 = $_POST['weigthAct8'];
        $ageWeaning = $_POST['ageW8'];
        $ageact8 = $_POST['ageAct8'];
        $stage = 'Ceba';
        $batchcod = $_POST['batch8'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca8."',
                                            ".$weightbirth8.",
                                            ".$weigthweaning8.",
                                            ".$weightact8.",
                                            ".$ageWeaning.",
                                            ".$ageact8.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/enterCattleC_3.php");   
}

public function regCattleC3(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();
        $birthdayca9 = $_POST['date9'];
        $weightbirth9 = $_POST['birthWeight9'];
        $weigthweaning9 = $_POST['weigthW9'];
        $weightact9 = $_POST['weigthAct9'];
        $ageWeaning = $_POST['ageW9'];
        $ageact9 = $_POST['ageAct9'];
        $stage = 'Ceba';
        $batchcod = $_POST['batch9'];
        $dateregister = $d;
        $sql = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca9."',
                                            ".$weightbirth9.",
                                            ".$weigthweaning9.",
                                            ".$weightact9.",
                                            ".$ageWeaning.",
                                            ".$ageact9.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/batchList.php");   
}

public function regGrowthUser(){
    $prom = new BatchProm();
    $d = date('d-m-Y');
    $user = $_POST['userid'];
    $growthcod = $prom->growth();
    $bactchcod = $prom->cod($user, $d);
    $growthdate = $d;
    $weightbirt = $prom->wBirth($bactchcod, $d);
    $weightpreweaning = $prom->wbeforeWeaning($bactchcod, $d);
    $weightweaning = $prom->wWeanig($bactchcod, $d);
    $weightpreceba = $prom->wPC($bactchcod, $d);
    $weightceba = $prom->wC($bactchcod, $d);
    $weightsacr = 500;
    $agepreweaning = $prom->agebeforeWeaning($bactchcod, $d);
    $ageWeaning = $prom->ageWeaning($bactchcod, $d);
    $agepreceba = $prom->agePC($bactchcod, $d);
    $ageceba = $prom->ageC($bactchcod, $d);
    $sql = "INSERT INTO GROWTH_USER VALUES (".$growthcod.",
                                            ".$bactchcod.",
                                            '".$growthdate."',
                                            ".$weightbirt.",
                                            ".$weightpreweaning.",
                                            ".$weightweaning.",
                                            ".$weightpreceba.",
                                            ".$weightceba.",
                                            ".$weightsacr.",
                                            ".$agepreweaning.",
                                            ".$ageWeaning.",
                                            ".$agepreceba.",
                                            ".$ageceba.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/menuUser.php");   
}


public function updatePerson(){
        $dnum = $_POST['userid'];
        $passwd = $_POST['passwd2'];
        $address = $_POST['address'];
        $phone = $_POST['phone'];
        $sql = "UPDATE PERSON SET passwd='".$passwd."', address='".$address."', phone=".$phone." 
                            where docnum=".$dnum.";";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/userProfile.php");      
    }

}

?>
